<!--
Copyright (c) 2019-2026, Hossein Moein
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.
* Neither the name of Hossein Moein and/or the DataFrame nor the
  names of its contributors may be used to endorse or promote products
  derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Hossein Moein BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<!DOCTYPE html>
<html>
<body>

  <table align="center" border="1">

    <tr bgcolor="lightblue">
      <th>Signature</th> <th>Description</th> <th>Parameters</th>
    </tr>
    <tr bgcolor="lightgrey">
      <td bgcolor="maroon"> <font color="white">
        <PRE><B>#include &lt;DataFrame/DataFrameStatsVisitors.h&gt;

template&lt;typename T, typename I = unsigned long&gt;
struct LowessVisitor;

// -------------------------------------

template&lt;typename T, typename I = unsigned long&gt;
using lowess_v = LowessVisitor&lt;T, I&gt;;
        </B></PRE></font>
      </td>
      <td>
        This is a “single action visitor”, meaning it is passed the whole data vector in one call and you must use the single_act_visit() interface.<BR><BR>
        This functor performs LOcally WEighted Scatterplot Smoothing<BR>
        A LOWESS function outputs smoothed estimates of dependent var (y) at the given independent var (x) values.<BR>
        This lowess function implements the algorithm given in the reference below using local linear estimates. Suppose the input data has N points. The algorithm works by estimating the <I>smooth</i> y<sub>i</sub> by taking the frac * N closest points to (x<sub>i</sub>, y<sub>i</sub>) based on their x values and estimating y<sub>i</sub> using a weighted linear regression. The weight for (x<sub>j</sub>, y<sub>j</sub>) is tricube function applied to |x<sub>i</sub> - x<sub>j</sub>|. If n_loop > 1, then further weighted local linear regressions are performed, where the weights are the same as above times the <I>lowess_bisquare</i> function of the residuals. Each iteration takes approximately the same amount of time as the original fit, so these iterations are expensive. They are most useful when the noise has extremely heavy tails, such as Cauchy noise. Noise with less heavy-tails, such as t-distributions with df > 2, are less problematic. The weights downgrade the influence of points with large residuals. In the extreme case, points whose residuals are larger than 6 times the median absolute residual are given weight 0.<BR>
        Delta can be used to save computations. For each x<sub>i</sub>, regressions are skipped for points closer than delta. The next regression is fit for the farthest point within delta of x<sub>i</sub> and all points in between are estimated by linearly interpolating between the two regression fits.<BR>
        Judicious choice of delta can cut computation time considerably for large data (N > 5000). A good choice is delta = 0.01 * range(independ_var). Some experimentation is likely required to find a good choice of frac and iter for a particular dataset.<BR>
        References<BR>
        ----------<BR>
        Cleveland, W.S. (1979) "Robust Locally Weighted Regression and Smoothing Scatterplots". Journal of the American Statistical Association 74 (368): 829-836.<BR><BR>
        <I>get_result()</I> returns the vector of fitted y values. There is also <I>get_residual_weights()</I> that returns the residual weights.<BR>
        The first column passed must be the dependent (y) or endog column. The second column passed must be the independent (x) or exog column.<BR><BR>

        <I>
        <PRE>
    explicit
    LowessVisitor (std::size_t loop_n = 3,
                   T frac = 2.0 / 3.0,
                   T delta = 0,
                   bool sorted = false);
        </PRE>
		</I>
<B>loop_n</B>: The noumber of iterations.<BR>
<B>frac</B>: Between 0 and 1. The fraction of the data used when estimating each y-value.<BR>
<B>delta</B>: Distance within which to use linear-interpolation instead of weighted regression.<BR>
<B>sorted</B>: Whether the x and y columns are already sorted in order of ascending x values.<BR>
      </td>
      <td width="12%">
        <B>T</B>: Column data type.<BR>
        <B>I</B>: Index type.
      </td>
    </tr>

  </table>

<pre style='color:#000000;background:#ffffff;'><span style='color:#800000; font-weight:bold; '>static</span> <span style='color:#800000; font-weight:bold; '>void</span> test_LowessVisitor<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span>  <span style='color:#800080; '>{</span>

    <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>cout</span> <span style='color:#808030; '>&lt;</span><span style='color:#808030; '>&lt;</span> <span style='color:#800000; '>"</span><span style='color:#0f69ff; '>\n</span><span style='color:#0000e6; '>Testing LowessVisitor{  } ...</span><span style='color:#800000; '>"</span> <span style='color:#808030; '>&lt;</span><span style='color:#808030; '>&lt;</span> <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>endl</span><span style='color:#800080; '>;</span>

    <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>vector</span><span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>unsigned</span> <span style='color:#800000; font-weight:bold; '>long</span><span style='color:#800080; '>></span>  idx <span style='color:#808030; '>=</span>
        <span style='color:#800080; '>{</span> <span style='color:#008c00; '>123450</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123451</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123452</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123453</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123454</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123455</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123456</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123457</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123458</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123459</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123460</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123461</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123462</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123466</span><span style='color:#808030; '>,</span>
          <span style='color:#008c00; '>123467</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123468</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123469</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123470</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123471</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123472</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>123473</span><span style='color:#808030; '>,</span>
        <span style='color:#800080; '>}</span><span style='color:#800080; '>;</span>
    <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>vector</span><span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span>         x_vec <span style='color:#808030; '>=</span> <span style='color:#800080; '>{</span>
        <span style='color:#008000; '>0.5578196</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>2.0217271</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>2.5773252</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>3.4140288</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>4.3014084</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>4.7448394</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>5.1073781</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>6.5411662</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>6.7216176</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>7.2600583</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>8.1335874</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>9.1224379</span><span style='color:#808030; '>,</span>
        <span style='color:#008000; '>1.9296663</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>2.3797674</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>3.2728619</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>4.2767453</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>5.3731026</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>5.6476637</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>8.5605355</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>8.5866354</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>8.7572812</span><span style='color:#808030; '>,</span>
    <span style='color:#800080; '>}</span><span style='color:#800080; '>;</span>
    <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>vector</span><span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span>         y_vec <span style='color:#808030; '>=</span> <span style='color:#800080; '>{</span>
        <span style='color:#008000; '>18.63654</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>103.49646</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>150.35391</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>190.51031</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>208.70115</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>213.71135</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>228.49353</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>233.55387</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>234.55054</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>223.89225</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>227.68339</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>223.91982</span><span style='color:#808030; '>,</span>
        <span style='color:#008000; '>168.01999</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>164.95750</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>152.61107</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>160.78742</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>168.55567</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>152.42658</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>221.70702</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>222.69040</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>243.18828</span><span style='color:#808030; '>,</span>
    <span style='color:#800080; '>}</span><span style='color:#800080; '>;</span>
    MyDataFrame                 df<span style='color:#800080; '>;</span>

    df<span style='color:#808030; '>.</span>load_data<span style='color:#808030; '>(</span><span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>move</span><span style='color:#808030; '>(</span>idx<span style='color:#808030; '>)</span><span style='color:#808030; '>,</span> <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_pair<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>indep_var</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> x_vec<span style='color:#808030; '>)</span><span style='color:#808030; '>,</span> <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_pair<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>dep_var</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> y_vec<span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    LowessVisitor<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span>   l_v<span style='color:#800080; '>;</span>

    df<span style='color:#808030; '>.</span>single_act_visit<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#808030; '>,</span> <span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>dep_var</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>indep_var</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> l_v<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#800000; font-weight:bold; '>auto</span>    actual_yfit <span style='color:#808030; '>=</span> <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>vector</span><span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span> <span style='color:#800080; '>{</span>
        <span style='color:#008000; '>68.1432</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>119.432</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>122.75</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>135.633</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>142.724</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>165.905</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>169.447</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>185.617</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>186.017</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>191.865</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>198.03</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>202.234</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>206.178</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>215.053</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>216.586</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>220.408</span><span style='color:#808030; '>,</span>
        <span style='color:#008000; '>226.671</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>229.052</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>229.185</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>230.023</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>231.657</span><span style='color:#808030; '>,</span>
    <span style='color:#800080; '>}</span><span style='color:#800080; '>;</span>

    <span style='color:#800000; font-weight:bold; '>for</span> <span style='color:#808030; '>(</span><span style='color:#603000; '>size_t</span> idx <span style='color:#808030; '>=</span> <span style='color:#008c00; '>0</span><span style='color:#800080; '>;</span> idx <span style='color:#808030; '>&lt;</span> actual_yfit<span style='color:#808030; '>.</span>size<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span> <span style='color:#808030; '>+</span><span style='color:#808030; '>+</span>idx<span style='color:#808030; '>)</span>
        assert<span style='color:#808030; '>(</span><span style='color:#603000; '>fabs</span><span style='color:#808030; '>(</span>l_v<span style='color:#808030; '>.</span>get_result<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>[</span>idx<span style='color:#808030; '>]</span> <span style='color:#808030; '>-</span> actual_yfit<span style='color:#808030; '>[</span>idx<span style='color:#808030; '>]</span><span style='color:#808030; '>)</span> <span style='color:#808030; '>&lt;</span> <span style='color:#008000; '>0.001</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#800000; font-weight:bold; '>auto</span>    actual_weights <span style='color:#808030; '>=</span> <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>vector</span><span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span> <span style='color:#800080; '>{</span>
        <span style='color:#008000; '>0.641773</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.653544</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.940738</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.865302</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.990575</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.971522</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.92929</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.902444</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.918228</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.924041</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.855054</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.824388</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.586045</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.945216</span><span style='color:#808030; '>,</span>
        <span style='color:#008000; '>0.94831</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.998031</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.999834</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.991263</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.993165</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.972067</span><span style='color:#808030; '>,</span> <span style='color:#008000; '>0.990308</span><span style='color:#808030; '>,</span>
    <span style='color:#800080; '>}</span><span style='color:#800080; '>;</span>

    <span style='color:#800000; font-weight:bold; '>for</span> <span style='color:#808030; '>(</span><span style='color:#603000; '>size_t</span> idx <span style='color:#808030; '>=</span> <span style='color:#008c00; '>0</span><span style='color:#800080; '>;</span> idx <span style='color:#808030; '>&lt;</span> actual_weights<span style='color:#808030; '>.</span>size<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span> <span style='color:#808030; '>+</span><span style='color:#808030; '>+</span>idx<span style='color:#808030; '>)</span>
        assert<span style='color:#808030; '>(</span><span style='color:#603000; '>fabs</span><span style='color:#808030; '>(</span>l_v<span style='color:#808030; '>.</span>get_residual_weights<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>[</span>idx<span style='color:#808030; '>]</span> <span style='color:#808030; '>-</span> actual_weights<span style='color:#808030; '>[</span>idx<span style='color:#808030; '>]</span><span style='color:#808030; '>)</span> <span style='color:#808030; '>&lt;</span> <span style='color:#008000; '>0.00001</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
<span style='color:#800080; '>}</span>
</pre>
<!--Created using ToHtml.com on 2020-10-29 17:21:12 UTC -->

  <img src="https://github.com/hosseinmoein/DataFrame/blob/master/docs/LionLookingUp.jpg?raw=true" alt="C++ DataFrame"
       width="200" height="150" style="float:right"/>

</body>
</html>

<!--
Local Variables:
mode:HTML
tab-width:4
c-basic-offset:4
End:
-->
